var msg = require('../config/system_message');
var constant = require('../config/constant');

module.exports = exports = {
    authorize: function (req, res, next) {
        if ('development' === process.env.NODE_ENV) {
            if (!req.session.account)
                req.session.account = {
                    "accountId": "53abe0047203fa7b2b000003",
                    "accountName": "风信子专家团",
                    "imgUrl": "http://182.92.79.214:3002/users/52b2f8dced07489c15000001/upload//%E4%B8%93%E5%AE%B6%E5%9B%A2%20(1).jpg",
                    "onlineStatus": 0,
                    "position": "",
                    "gender": 0,
                    "hospital": "",
                    "recommendGrade": 5,
                    "des": "",
                    "status": 1,
                    "qrcode": "http://img.qrcode.fengxz.com.cn/2015-03-12/60379b05-c2d0-41f3-9b5e-1ae8e6082702.png",
                    "department": 1,
                    "verify": 1,
                    "stopService": false,
                    "service": {
                        "scope": 2,
                        "free": {
                            "dailyNum": 1
                        },
                        "charge": {
                            "dailyNum": 3,
                            "onlineCost": 6.0,
                            "telephoneCost": 10.0,
                            "monthlyNum": 100,
                            "monthlyCost": 150.0,
                            "monthlyMin": 1,
                            "monthlyMax": 9
                        }
                    },
                    "adept": "妇科疑难病症的诊治, 妇科各种手术, 如腹式、阴式手术、子宫及附件肿瘤、子宫内膜异位症、子宫脱垂、张力性尿失禁、先天性生殖道畸形及不孕症的手术治疗 。",
                    "location": {
                        "type": "Point",
                        "coordinates": [116.4954614639, 39.9087157541]
                    }
                };
            next();
        } else {
            if (!req.session.account) {
                res.createFailure({code: msg.CODE.AUTH_ERROR, message: msg.ERR_MSG.AUTH});
            } else {
                // TODO: 账号无效与认证未通过(verify)
                if (req.session.account.status == constant.AccountStatus.INVALID) {
                    req.session.destroy();
                    res.createFailure({code: msg.CODE.AUTH_ERROR, message: msg.ERR_MSG.AUTH});
                    return;
                }

                //
                next();
            }
        }
    }
};